package short_semester.questionnaire_planet.service.editService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import short_semester.questionnaire_planet.dao.FillBlankQuestionDao;
import short_semester.questionnaire_planet.dao.FillBlankQuestionRecordDao;
import short_semester.questionnaire_planet.dao.QuestionSequenceDao;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.request.editRequest.EditFillBlankQuestionBody;
import short_semester.questionnaire_planet.response.CommonResponse;
import short_semester.questionnaire_planet.response.editResponse.RelationResponse;

import java.util.List;

@Service
public class FillBlankQuestionService {
    @Autowired
    FillBlankQuestionDao questionDao;
    
    @Autowired
    QuestionSequenceDao sequenceDao;

    @Autowired
    FillBlankQuestionRecordDao recordDao;
    @Autowired
    RelationService relationService;
    
    public List<FillBlankQuestion> getQuestions(Questionnaire questionnaire){
        return questionDao.findByQuestionnaire(questionnaire);
    }
    
    public FillBlankQuestion getQuestion(Integer questionId){
        return questionDao.getById(questionId);
    }
    
    public CommonResponse getQuestionResponse(Integer id){
        CommonResponse response = new CommonResponse();
        try {
            
            FillBlankQuestion question = questionDao.getById(id);
            response.setData(question);
        }catch (Exception e){
            response.error(e.getMessage());
        }
        
        return response;
    }

    public int getQuestionNum(Questionnaire questionnaire){
        return questionDao.countByQuestionnaire(questionnaire);
    }

    public int addQuestion(Questionnaire questionnaire,Integer sequence,String qtype){
        FillBlankQuestion question = new FillBlankQuestion(qtype);
        question.setQuestionnaire(questionnaire);
        question.setSequence(sequence);
        question = questionDao.save(question);

        

        QuestionSequence questionSequence = new QuestionSequence();
        questionSequence.setQuestionnaire(questionnaire);
        questionSequence.setSequence(sequence);
        questionSequence.setType(question.getType());
        questionSequence.setQuestionId(question.getId());
        sequenceDao.save(questionSequence);
        return question.getId();
    }
    
    public void deleteQuestion(Integer questionId){
        QuestionSequence questionSequence = sequenceDao.findByTypeAndQuestionId("completion", questionId);
        sequenceDao.delete(questionSequence);

        relationService.relationDao.deleteByQuestionIdAndType(questionId,"completion");
//        relationService.relationDao.deleteByRelationIdAndRelationType(questionId,question.getType());
        
        questionDao.deleteById(questionId);
    }
    
    public FillBlankQuestion getQuestionById(Integer questionId){
        return questionDao.getById(questionId);
    }
    
    public void setSequenceById(Integer questionId,Integer sequence){
        FillBlankQuestion question = questionDao.getById(questionId);
        question.setSequence(sequence);
        questionDao.save(question);

        QuestionSequence questionSequence = sequenceDao.findByTypeAndQuestionId(question.getType(), questionId);
        questionSequence.setSequence(sequence);
        sequenceDao.save(questionSequence);
    }
    
    public CommonResponse editQuestion(EditFillBlankQuestionBody body){
        CommonResponse response = new CommonResponse();
        try {
            
            FillBlankQuestion question = questionDao.getById(body.getId());
            question.setValueFrom(body);
            questionDao.save(question);
            
        }catch (Exception e){
            response.error(e.getMessage());
        }
        
        return response;
    }
    
    public void copyQuestion(Questionnaire questionnaire,Questionnaire oldQuestionnaire){
        for (FillBlankQuestion question : questionDao.findByQuestionnaire(oldQuestionnaire)) {
            FillBlankQuestion fillBlankQuestion = new FillBlankQuestion(question);
            fillBlankQuestion.setQuestionnaire(questionnaire);
            fillBlankQuestion = questionDao.save(fillBlankQuestion);
            
            QuestionSequence questionSequence = new QuestionSequence(fillBlankQuestion);
            sequenceDao.save(questionSequence);
        }
    }

    //彻底删除题目，包括选项与答题
    public void completeDelete(FillBlankQuestion fillBlankQuestion){
        recordDao.deleteByFillBlankQuestion(fillBlankQuestion);
        QuestionSequence questionSequence = sequenceDao.findByTypeAndQuestionId(fillBlankQuestion.getType(), fillBlankQuestion.getId());
        sequenceDao.delete(questionSequence);
        questionDao.delete(fillBlankQuestion);

    }
    public void deleteAnswer(FillBlankQuestion fillBlankQuestion){
        recordDao.deleteByFillBlankQuestion(fillBlankQuestion);
    }

    //获取关联关系
    public RelationResponse getRelation(Integer questionId){
        FillBlankQuestion fillBlankQuestion = questionDao.getById(questionId);
        return relationService.getRelation(fillBlankQuestion);
    }

    //添加姓名的填空题
    public void addNameQuestion(Questionnaire questionnaire,Integer sequence){
        FillBlankQuestion fillBlankQuestion = new FillBlankQuestion();
        fillBlankQuestion.setTitle("姓名");
        fillBlankQuestion.setQuestionnaire(questionnaire);
        fillBlankQuestion.setSequence(sequence);

        questionDao.save(fillBlankQuestion);
        QuestionSequence questionSequence = new QuestionSequence(fillBlankQuestion);
        sequenceDao.save(questionSequence);
    }

    //添加电话号码的填空题
    public void addPhoneNumQuestion(Questionnaire questionnaire){
        FillBlankQuestion fillBlankQuestion = new FillBlankQuestion();
        fillBlankQuestion.setTitle("电话号码");
        fillBlankQuestion.setQuestionnaire(questionnaire);
        fillBlankQuestion.setSequence(1);

        questionDao.save(fillBlankQuestion);
        QuestionSequence questionSequence = new QuestionSequence(fillBlankQuestion);
        sequenceDao.save(questionSequence);
    }

    //添加学号的填空题
    public void addStudentIdQuestion(Questionnaire questionnaire){
        FillBlankQuestion fillBlankQuestion = new FillBlankQuestion();
        fillBlankQuestion.setTitle("学号");
        fillBlankQuestion.setQuestionnaire(questionnaire);
        fillBlankQuestion.setSequence(0);
<<<<<<< HEAD
=======

>>>>>>> refs/remotes/origin/dev

        questionDao.save(fillBlankQuestion);
        QuestionSequence questionSequence = new QuestionSequence(fillBlankQuestion);
        sequenceDao.save(questionSequence);
    }

    //添加定位题
    public void addLocationQuestion(Questionnaire questionnaire){
        FillBlankQuestion fillBlankQuestion = new FillBlankQuestion();
        fillBlankQuestion.setTitle("定位");
        fillBlankQuestion.setQuestionnaire(questionnaire);
        fillBlankQuestion.setSequence(5);
        fillBlankQuestion.setQtype("card");

        questionDao.save(fillBlankQuestion);
        QuestionSequence questionSequence = new QuestionSequence(fillBlankQuestion);
        sequenceDao.save(questionSequence);


    }


}
